//
//  SAPrintSkuSizeCrosswise.m
//  Saas
//
//  Created by 杨荣 on 2020/6/8.
//  Copyright © 2020 bing. All rights reserved.
//

#import "SAPrintSkuSizeCrosswise.h"
#import "SASalesOrderViewListModel.h"


@implementation SAPrintSkuSizeCrosswise

- (NSArray <NSString *>*)printSkuColorSizeLineWithProductSkuArray:(NSArray <SASalesOrderViewListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    
    NSMutableArray <NSString *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.16*printerByteWidth),@(0.3*printerByteWidth),@(0.39*printerByteWidth),@(0.48*printerByteWidth),@(0.57*printerByteWidth),@(0.66*printerByteWidth),@(0.76*printerByteWidth),@(0.86*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    
    // 商品sku
    NSArray <SASalesOrderViewListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SASalesOrderViewListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    
    NSMutableArray *sizeNameTotalArray = [NSMutableArray array];// 所有商品的尺码集合
    for (SASalesOrderViewListModel *product in procutArray) {
        NSArray *sizeNameArray = [product.toPendSkuAttrs valueForKeyPath:@"@distinctUnionOfObjects.sizeName"];
        [sizeNameTotalArray addObjectsFromArray:sizeNameArray];// 每一个商品的尺码集合
    }
    NSArray *sizeArray = [sizeNameTotalArray valueForKeyPath:@"@distinctUnionOfObjects.self"];// 去重，得到所有商品的尺码集合
    
    NSInteger rowSizeCountMax = 4;// 一行最多打印4个尺码
    NSMutableArray *sizeArrayArray = [NSMutableArray array];// 一个区商品有多少行尺码，每行尺码的集合
    NSInteger rowSizeCount = rowSizeCountMax;
    if (sizeArray.count <= rowSizeCount) {
        rowSizeCount = sizeArray.count;
        [sizeArrayArray addObject:sizeArray];
    }else {
        NSInteger rowCount = sizeArray.count/rowSizeCount;
        NSInteger x = sizeArray.count%rowSizeCount;
        if (x != 0) {
            rowCount = rowCount+1;
        }
        for (NSInteger i = 0; i < rowCount; i++) {
            if (i == rowCount-1) {
                if (x == 0) {
                    rowSizeCount = rowSizeCountMax;
                }else {
                    rowSizeCount = x;
                }
            }
            NSArray *rowSizeArray = [sizeArray subarrayWithRange:NSMakeRange(rowSizeCountMax*i, rowSizeCount)];
            [sizeArrayArray addObject:rowSizeArray];
        }
    }
    
    // 1、按照尺码分组
    for (NSArray *sizeArray in sizeArrayArray) {
        // 分割线
        [printStrArray addObject:partingLine];
        // 标题栏
        NSMutableArray *array = [NSMutableArray array];
        [array addObject:@{@"商品":originXArray[0]}];
        [array addObject:@{@"颜色":originXArray[1]}];
        for (NSInteger j = 0; j < sizeArray.count; j++) {
            NSString *size = sizeArray[j];
            NSString *sizeOriginX = [NSString stringWithFormat:@"%zd",[originXArray[2+j] integerValue]];
            [array addObject:@{size:sizeOriginX}];
        }
        [array addObject:@{@"单价":originXArray[2+rowSizeCountMax+1]}];
        [array addObject:@{@"小计":originXArray[2+rowSizeCountMax+2]}];
        NSString *titleStr = [NSString getPrintTextWithTextInfoArray:array font:(NSStringFontSmall)];
        [printStrArray addObject:titleStr];
        // 分割线
        [printStrArray addObject:partingLine];
        
        
        // 商品sku
        BOOL isShowReplenishmentReturn = [SAOverallSetting shareInstance].isShowReplenishmentReturn;// 全局补货标识是否开启
        NSArray <SASalesOrderViewListModel *>*sectionProductArray = [self getProductSkuArrayBySizeWithProductSkuArray:procutArray sizeArray:sizeArray];// 某一组尺码下的所有sku
        for (SASalesOrderViewListModel *product in sectionProductArray) {
            NSArray <SASalesOrderViewListModel *>*colorPriceProductArray = [self getProductSkuArrayByColorPriceWithProduct:product sizeArray:sizeArray];// 某一组尺码同颜色同单价下的所有sku
            NSString *title = [NSString stringWithFormat:@"%@,%@",product.productName,product.productCode];
            title = [title getSubStringWithByteWidthMax:[originXArray[1] integerValue] font:(NSStringFontSmall)];
            for (SASalesOrderViewListModel *product2 in colorPriceProductArray) {
                NSMutableArray *array = [NSMutableArray array];
                [array addObject:@{title:originXArray[0]}];// 名称
                [array addObject:@{product2.toPendSkuAttrs[0].colourName:originXArray[1]}];// 颜色
                title = @"";
                
                CGFloat total = 0;// 小计
                for (NSInteger k = 0; k < sizeArray.count; k++) {
                    NSString *size = sizeArray[k];
                    SASalesOrderToPendSkuList *filterSku = [product2.toPendSkuAttrs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
                        SASalesOrderToPendSkuList *model = evaluatedObject;
                        return [model.sizeName isEqualToString:size];
                    }]].firstObject;
                    if (filterSku) {
                        NSString *sizeOriginX = [NSString stringWithFormat:@"%zd",[originXArray[2+k] integerValue]];
                        
                        NSString *stock = filterSku.stock;// 数量
                        if (isShowReplenishmentReturn && [filterSku.replenishType isEqualToString:@"1"]) {
                            stock = [NSString stringWithFormat:@"%@+",stock];// 增加补货标识+
                        }
                        [array addObject:@{stock:sizeOriginX}];// 数量
                        
                        total += [filterSku.stock intValue] * product2.toPendSkuAttrs[0].price.floatValue/100;
                    }
                }
                [array addObject:@{[NSString stringWithFormat:@"%.2f",product2.toPendSkuAttrs[0].price.floatValue/100].getEffectivePointString:originXArray[2+rowSizeCountMax+1]}];// 单价
                [array addObject:@{[NSString stringWithFormat:@"%.2f",total].getEffectivePointString:originXArray[2+rowSizeCountMax+2]}];// 小计
                NSString *titleStr = [NSString getPrintTextWithTextInfoArray:array font:(NSStringFontSmall)];
                [printStrArray addObject:titleStr];
            }
        }
    }
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 特殊费用
    NSArray <SASalesOrderViewListModel *>*specailFeeArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SASalesOrderViewListModel *model = evaluatedObject;
        return model.productCode.length == 0;
    }]];
    
    if (specailFeeArray.count > 0) {
        for (SASalesOrderViewListModel *specailFee in specailFeeArray) {
            NSString *str = [NSString getPrintTextWithLeftText:specailFee.productName rightText:specailFee.toPendSkuAttrs[0].subtotal.getEffectivePointString byteWidth:printerByteWidth];
            [printStrArray addObject:str];
            
        }
        // 分割线
        [printStrArray addObject:partingLine];
    }
    return printStrArray;
}


@end
